<template>
  <div>
    <el-form ref="form" :model="adminUser" clearable filterable label-width="100px">
      <el-form-item label="旧密码:">
        <el-input v-model="adminUser.oldPassword" type="password" />
      </el-form-item>
      <el-form-item label="新密码:">
        <el-input v-model="adminUser.password" type="password" />
      </el-form-item>
      <el-form-item label="确认密码:">
        <el-input v-model="adminUser.confirmPassword" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import adminApi from '@/api/admin'
import md5 from 'js-md5'

export default {
  data() {
    return {
      adminUser: {	
          headerUrl: "",
          id: 0,
          name: "",
          oldPassword: "",
          password: "",
          confirmPassword:"",
          username: ""
        },
    }
  },
  methods: {
    onSubmit() {
      if(this.adminUser.confirmPassword===this.adminUser.password){
        var admin = this.$store.getters.userInfo
        this.adminUser.id = admin.id
        this.adminUser.username = admin.username
        this.adminUser.oldPassword = md5(this.adminUser.oldPassword)
        this.adminUser.password = md5(this.adminUser.password)
        adminApi.updatePwd(this.adminUser).then(res => {
          this.adminUser.oldPassword = ''
          this.adminUser.confirmPassword = ''
          this.adminUser.password = ''
          this.$message.success(res.data)
          this.$emit('closeUpdateDialog')
        })
      }else{
        this.$message.error("新密码与确认密码不一致!")
      }
    }
  }
}
</script>
